package com.cro.prison.common;

import com.cro.prison.entity.constant.IDENTITYENUM;
import com.cro.prison.entity.constant.MESSAGESTATUSENUM;
import com.cro.prison.entity.constant.MESSAGETYPEENUM_CRIMINAL;
import com.cro.prison.entity.constant.MESSAGETYPEENUM_POLICE;
import com.cro.prison.entity.pojo.Depart;
import com.cro.prison.entity.pojo.Device;
import com.cro.prison.entity.pojo.ErrorMessage;
import com.cro.prison.entity.pojo.User;
import com.cro.prison.entity.vo.ErrorMessageVo;
import com.cro.prison.utils.DateUtils;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ErrorMessageMethod {

    public static Long rootId = 0L;

    public static List<ErrorMessage> getMessageVoListByDepartId(List<ErrorMessage> list
            , List<Depart> departList
            ,Long departId) {

        //map ：key-》departId value—》depart，主要用于静态方法查询当前departId的最高级departId
        HashMap<Long,Depart> map = new HashMap<>();

        //key所有最高级depart的departId，value前端返回的item
        HashMap<Long, List<ErrorMessage>> resultMap = new HashMap<>();

        for (Depart depart : departList) {
            map.put(depart.getId(),depart);
            if(ErrorMessageMethod.rootId.equals(depart.getPid())){
                resultMap.put(depart.getId(),new ArrayList<>());
            }
        }
        for (ErrorMessage errorMessage : list) {
            Long parentId = findParent(errorMessage.getDepartId(), map);
            List<ErrorMessage> errorMessages = resultMap.get(parentId);
            errorMessages.add(errorMessage);
        }
        List<ErrorMessage> errorMessages = resultMap.get(departId);
        return errorMessages;
    }

    //根据当前departId找到他的最高级departid
    public static Long findParent(Long departId,HashMap<Long, Depart> map){
        Depart depart = map.get(departId);
        if(depart.getPid()==0){
            return departId;
        }else{
            return findParent(depart.getPid(),map);
        }
    }
    /**
     * 此方法用于将实体类列表errorMessageList转换成前端需要的ErrorMessageVoList
     * @param list
     * userList
     * departList
     * deviceList
     * @return voList
     */
    public static List<ErrorMessageVo> getMessageVoList(List<ErrorMessage> list
                                                        , List<User> userList
                                                        , List<Depart> departList
                                                        , List<Device> deviceList) {
        //设置前置集合，部分集合设计 lambda 表达式
        List<ErrorMessageVo> voList = new ArrayList<>(list.size());
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getUid, u -> u));
        Map<Long, Depart> departMap = departList.stream().collect(Collectors.toMap(Depart::getId, d -> d));
        Map<Long, Device> deviceMap = deviceList.stream().collect(Collectors.toMap(Device::getId, d -> d));

        for (ErrorMessage errorMessage : list) {
            // 基于实体自身的转换 如 时间 和 枚举类型变化
            ErrorMessageVo vo = formatMessage(errorMessage);
            Long deviceId = vo.getDeviceId();
            Long userId = errorMessage.getUserId();
            Long departId = errorMessage.getDepartId();
            if(null != deviceId){
                vo.setSrc(deviceMap.get(deviceId).getSrc());
            }
            if(null != userId){
                User user = userMap.get(userId);
                vo.setTransactor(user.getUsername());
            }
            //位置信息（3号楼-3层-303）
            if(null !=  departId){
                formatPlace(vo,departMap,departId);
            }
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 根据 departMap 和 departId 对vo的place 属性进行转换
     * @param vo
     * @param departMap
     * @param departId
     * @return
     */
    public static ErrorMessageVo formatPlace(ErrorMessageVo vo,Map<Long, Depart> departMap,Long departId){
        Depart currentDepart = departMap.get(departId);
        if(currentDepart!=null){
            String place = currentDepart.getName();
            while(currentDepart.getPid()!=0){
                currentDepart = departMap.get(currentDepart.getPid());
                place = currentDepart.getName() +"-" + place;
            }
            vo.setPlace(place);
        }
        return vo;
    }
    public static ErrorMessageVo formatMessage(ErrorMessage errorMessage) {
        ErrorMessageVo vo = new ErrorMessageVo();
        BeanUtils.copyProperties(errorMessage, vo);
        vo.setDate(DateUtils.detailFormat(errorMessage.getCreateTime()));
        Integer identity = errorMessage.getIdentity();
        Integer type = errorMessage.getType();
        Integer status = errorMessage.getStatus();
        if(isEqualCode(identity,IDENTITYENUM.PRISONER.getCode())){
            vo.setIdentity(IDENTITYENUM.PRISONER.getName());
            setMessageCriminalType(vo,type,MESSAGETYPEENUM_CRIMINAL.values());
        }
        if(isEqualCode(identity,IDENTITYENUM.POLICE.getCode())){
            vo.setIdentity(IDENTITYENUM.POLICE.getName());
            setMessagePoliceType(vo,type,MESSAGETYPEENUM_POLICE.values());

        }
        if(isEqualCode(identity,IDENTITYENUM.VISITOR.getCode())){
            vo.setIdentity(IDENTITYENUM.VISITOR.getName());
        }

//        if(isEqualCode(type, MESSAGETYPEENUM_CRIMINAL.FIGHT.getCode())){
//            vo.setType(MESSAGETYPEENUM_CRIMINAL.FIGHT.getName());
//        }
//        if(isEqualCode(type, MESSAGETYPEENUM_CRIMINAL.CLIMBING.getCode())){
//            vo.setType(MESSAGETYPEENUM_CRIMINAL.CLIMBING.getName());
//        }
//        if(isEqualCode(type, MESSAGETYPEENUM_CRIMINAL.SQUATS.getCode())){
//            vo.setType(MESSAGETYPEENUM_CRIMINAL.SQUATS.getName());
//        }
//        if(isEqualCode(type, MESSAGETYPEENUM_CRIMINAL.GUARD_AREA.getCode())){
//            vo.setType(MESSAGETYPEENUM_CRIMINAL.GUARD_AREA.getName());
//        }

        if(isEqualCode(status, MESSAGESTATUSENUM.UNSOLVED.getCode())){
            vo.setStatus(MESSAGESTATUSENUM.UNSOLVED.getName());
        }
        if(isEqualCode(status, MESSAGESTATUSENUM.SOLVED.getCode())){
            vo.setStatus(MESSAGESTATUSENUM.SOLVED.getName());
        }
        return vo;
    }
    public static void setMessageCriminalType(ErrorMessageVo vo,Integer type,MESSAGETYPEENUM_CRIMINAL[] values){
        for(int i = 0;i<values.length;i++){
            if(isEqualCode(type,values[i].getCode())){
                vo.setType(values[i].getName());
            }
        }
    }
    public static void setMessagePoliceType(ErrorMessageVo vo,Integer type,MESSAGETYPEENUM_POLICE[] values){
        for(int i = 0;i<values.length;i++){
            if(isEqualCode(type,values[i].getCode())){
                vo.setType(values[i].getName());
            }
        }
    }
    public static boolean isEqualCode(Integer x,Integer code){
        return code.equals(x);
    }


}
